package com.cskaoyan.service;

import com.cskaoyan.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author stone
 * @date 2022/08/04 14:27
 */
@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    AccountMapper accountMapper;

    //@Transactional
    //@Transactional(timeout = 5, readOnly = true)
    @Transactional(noRollbackFor = ArithmeticException.class)
    @Override
    public void transfer(int fromId, int destId, Integer money) {
        // 先查询
        Integer fromMoney = accountMapper.selectMoneyById(fromId);
        Integer destMoney = accountMapper.selectMoneyById(destId);
        // 计算转账后的money
        fromMoney -= money;
        destMoney += money;
        // 更新money
        int from = accountMapper.update(fromId, fromMoney);
        int i = 1 / 0;
        int dest = accountMapper.update(destId, destMoney);
    }
}
